﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.DataAnnotations
{
    /// <summary>
    /// 
    /// </summary>
    public class PaymentPasswordAttribute : ValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        public PaymentPasswordAttribute()
        {
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            var temp = value.ToString();
            if (temp.Length < 6)
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "请输入6位有效{0}！";
                return false;
            }
            if (temp.Length > 20)
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "请输入6位有效{0}！";
                return false;
            }
            if (!Regex.IsMatch(temp, @"^\d{6}$"))
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "{0}必须是6位数字！";
                return false;
            }
            return true;
        }
    }
}
